
import sys
# require python >= 3.12 
# [monitor api doc](https://docs.python.org/3.13/library/sys.monitoring.html#monitoring-event-LINE)
monitoring = sys.monitoring


def hello(x):
    x += 100
    # Let's change the value of x before return
    return x

monitoring.use_tool_id(0,'hello')

monitoring.set_local_events(0, hello.__code__, monitoring.events.LINE )

def line_callback(code, line_number: int):
    if line_number == 11: # how to automatically get line number? see demo3.py
        frame = sys._getframe(1)
        exec("x = 2",frame.f_globals,frame.f_locals) #this is the code to change the value of x
        import ctypes # what these code do? see main.md and demo2.py
        LocalsToFast = ctypes.pythonapi.PyFrame_LocalsToFast  
        LocalsToFast.argtypes = [ctypes.py_object, ctypes.c_int]  
        LocalsToFast(frame, 0)


monitoring.register_callback(0,monitoring.events.LINE,line_callback)

# okay now we change the value of x before return
assert(hello(0) == 2)
print(hello(0)) 
